package com.ctsi.ssdc.service.impl.front;

import com.ctsi.ssdc.mapper.EconomicIndicatorsMapper1;
import com.ctsi.ssdc.service.front.EconomicIndicatorsService1;
import com.ctsi.ssdc.entity.EconomicIndicators;
import com.ctsi.ssdc.entity.GeneralPublicBudgetRevenue;
import com.ctsi.ssdc.entity.MinEconomicIndicators;
import com.ctsi.ssdc.entity.QuarterlyGrossRegionalProduct;
import com.ctsi.ssdc.utils.MathUtil;
import com.ctsi.ssdc.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class EconomicIndicatorsService1Impl implements EconomicIndicatorsService1 {
    @Autowired
    EconomicIndicatorsMapper1 economicIndicatorsMapper1;


    /**
     * 季度地区生产总值
     * <p>
     * <p>
     * //季度
     * private int quarter;
     * //季度值
     * private double quarterValue;
     * //季度累计
     * private double quarterAccumulate;
     * //季度同比
     * private double quarterOnQuarter;
     *
     * @param year 年份（int）
     * @return List<QuarterlyGrossRegionalProduct>
     */
    @Override
    public List<QuarterlyGrossRegionalProduct> getQuarterlyGrossRegionalProduct(int year) {
        List<EconomicIndicators> list1 = economicIndicatorsMapper1.findQuarterlyCumulativeProduction(year);
        List<QuarterlyGrossRegionalProduct> list2 = new ArrayList<>();
        Iterator<EconomicIndicators> it = list1.iterator();
        while (it.hasNext()) {
            EconomicIndicators economicIndicators = it.next();
            // 循环遍历数据,获取各季度累计值填充到数组中
            if (TimeUtil.month(economicIndicators.getStatisticalTime()) == 3) {

//                    quarterValueList[1] = economicIndicators.getAccumulate() - quarterValueList[0];
                list2.add(new QuarterlyGrossRegionalProduct(1,
                        MathUtil.round(economicIndicators.getAccumulate(), 2),
                        MathUtil.round(economicIndicators.getAccumulate(), 2),
                        MathUtil.round(economicIndicators.getMonthOnMonth(), 1)));
            } else if (TimeUtil.month(economicIndicators.getStatisticalTime()) == 6 ||
                    TimeUtil.month(economicIndicators.getStatisticalTime()) == 9 ||
                    TimeUtil.month(economicIndicators.getStatisticalTime()) == 12) {
                list2.add(new QuarterlyGrossRegionalProduct(TimeUtil.month(economicIndicators.getStatisticalTime())/3,
                        economicIndicators.getAccumulate()==0?0:MathUtil.round(economicIndicators.getAccumulate() - list2.get(TimeUtil.month(economicIndicators.getStatisticalTime())/3-2).getQuarterAccumulate(),  2),
                        MathUtil.round(economicIndicators.getAccumulate(), 2),
                        MathUtil.round(economicIndicators.getMonthOnMonth() ,1)));

            }
        }
        return list2;
    }

    /**
     * 季度地区累计生产总值
     *
     * @param year 年份（int）
     * @return List<QuarterlyGrossRegionalProduct>
     */
    @Override
    public List<QuarterlyGrossRegionalProduct> getAccumulatedQuarterlyGrossRegionalProduct(int year) {
        List<QuarterlyGrossRegionalProduct> list = this.getQuarterlyGrossRegionalProduct(year);
        double total = 0;
        double tmp = 0;
        Iterator<QuarterlyGrossRegionalProduct> it = list.iterator();
        while (it.hasNext()) {
            QuarterlyGrossRegionalProduct quarterlyGrossRegionalProduct = it.next();
            tmp = quarterlyGrossRegionalProduct.getQuarterValue();
            quarterlyGrossRegionalProduct.setQuarterValue(tmp + total);
            total += tmp;
        }
        return list;
    }

    /**
     * 年度任务及单位
     *
     * @param year       年份（int）
     * @param indicators 指标 （String）
     * @return EconomicIndicators
     */
    @Override
    public EconomicIndicators getAnnualTasks(int year, String indicators) {
        return economicIndicatorsMapper1.findAnnualTasks(year, indicators);
    }

    /**
     * 一般公共预算收入
     *
     * @param year 年份（int）
     * @return List<GeneralPublicBudgetRevenue>
     */
    @Override
    public List<GeneralPublicBudgetRevenue> getGeneralPublicBudgetRevenue(int year) {
        return economicIndicatorsMapper1.findGeneralPublicBudgetRevenue(year);
    }

    /**
     * 全社会固定资产投资
     * 工业投资
     * 规上工业总产值
     * 社会消费品零售总额
     * 进出口总额
     * 出口总额
     * 实际使用外资
     *
     * @param year       年份（int）
     * @param indicators 指标 (String)
     * @return List<MinEconomicIndicators>
     */
    @Override
    public List<MinEconomicIndicators> getEconomicIndicatorsByIndicators(int year, String indicators) {
//        if(indicators == "出口总额" || indicators == "工业投资"){
//            indicators = "#" + indicators;
//        }
        return economicIndicatorsMapper1.findEconomicIndicatorsByIndicators(year, indicators);
    }

    @Override
    public int getAllqy(int year, int month) {
        return economicIndicatorsMapper1.findAllqy(year, month);
    }
}
